package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.enhe.gck.communal.PublicConstant;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.communal.PublicReturn;
import com.enhe.gck.item.SupplyOrderHWANE16829Item;
import com.enhe.gck.item.WovenBagHWANE21190Item;
import com.enhe.gck.pojo.WovenBag;
import com.enhe.gck.pojo.WovenBagRecord;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.common.RemoveRecordInfo;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import com.enhe.gck.CarbonBaseConstant; //模型Code
import com.enhe.gck.CarbonEnumKeyValue; //枚举字典信息
import com.enhe.gck.CarbonRelationType; //关系类型Code
import com.enhe.gck.item.WovenBagRecordHWANE21270Item; // 模型属性Code

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Repository(value = "hwane21270366042330866425862")
@Slf4j
public class WovenBagRecordHWANE21270BNB implements SecondRoundImproveFuncGroup, FuseCallBackFuncGroup, RemoveFuncGroup {

	@Override
	public synchronized void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		WovenBagRecord info=proRecord.getBean(WovenBagRecord.class);
		if(proRecord.isNew()){
			if(StrUtil.isNotBlank(context.getUserCode())){
				PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_编织袋记录_创建账户_系统用户,context.getUserCode());
			}
		}

		if(info.getIsConfirm()!=null&&info.getIsConfirm()){
			if(StrUtil.isNotBlank(context.getUserCode())){
				PublicMethod.saveAddNow(improveResultFactory).addRelation(CarbonRelationType.RR_编织袋记录_确认账户_系统用户,context.getUserCode());
			}
			PublicMethod.saveAddNow(improveResultFactory).putAttribute(WovenBagRecordHWANE21270Item.基本属性组_确认时间, LocalDateTime.now());
		}
	}

	@Override
	public boolean afterFusition(FuncGroupContext funcGroupContext, FuseContext fuseContext, String s) {
		resetting(s);
		return true;
	}

	@Override
	public boolean beforeRemove(FuncGroupContext funcGroupContext, RemoveRecordInfo removeRecordInfo, DeleteMessageFactory defDeleteMessageFactory) {
		Collection<String> list=removeRecordInfo.getRecordCodes();
		log.info("删除业务开始:{}",list);
		boolean isOk=true;
		if(list!=null){
			for (String x : list) {
				WovenBagRecord info=PublicMethod.getInfo(CarbonBaseConstant.编织袋记录_bzdjl,x,WovenBagRecord.class);
				if(info!=null&&info.getIsCancel()!=null&&!info.getIsCancel()){
					PublicMethod.returnMessage(defDeleteMessageFactory, PublicReturn.DELETE_2);
					isOk=false;
				}
			}
		}
		log.info("删除业务结束");
		return isOk;
	}

	/**
	 * 重置数量余额
	 */
	private void resetting(String s){
		WovenBagRecord info=PublicMethod.getInfo(CarbonBaseConstant.编织袋记录_bzdjl,s,WovenBagRecord.class,2);
		if(info!=null&&info.getWovenBag()!=null){
			RecordQueryProConJunctionFactory queryR=PublicMethod.query(CarbonBaseConstant.编织袋记录_bzdjl)
					.addEqual(WovenBagRecordHWANE21270Item.基本属性组_确认,true)
					.addEqual(WovenBagRecordHWANE21270Item.基本属性组_作废,false)
					;
			queryR.getRelationCriterionFactory(CarbonBaseConstant.编织袋管理_bzdgl)
					.setInRelationTypes(CarbonRelationType.RR_编织袋记录_名称_编织袋管理)
					.setInGuestCodes(info.getWovenBag().getRecordCode());
			List<WovenBagRecord> list=queryR.goBack().queryBeans(WovenBagRecord.class,1);
			if(list!=null){
				int use=0;
				int repair=0;
				int cancel=0;
				int worn=0;

				int surplus=0;
				for(WovenBagRecord record:list){
					if(String.valueOf(CarbonEnumKeyValue.编织袋记录类型_使用_sy).equals(record.getType())){
						use=use+record.getNumber();
					}
					if(String.valueOf(CarbonEnumKeyValue.编织袋记录类型_补包_bb).equals(record.getType())){
						repair=repair+record.getNumber();
					}
					if(String.valueOf(CarbonEnumKeyValue.编织袋记录类型_退包_tb).equals(record.getType())){
						cancel=cancel+record.getNumber();
					}
					if(String.valueOf(CarbonEnumKeyValue.编织袋记录类型_破损_ps).equals(record.getType())){
						worn=worn+record.getNumber();
					}
				}
				surplus=info.getWovenBag().getNumber()-use-repair+cancel-worn;
				ProRecordBuilder update= PublicMethod.update(CarbonBaseConstant.编织袋管理_bzdgl,info.getWovenBag().getRecordCode());
//				int saveSurplus=Math.max(surplus, 0);
				update.putAttribute(WovenBagHWANE21190Item.基本属性组_使用数,use)
						.putAttribute(WovenBagHWANE21190Item.基本属性组_补包数,repair)
						.putAttribute(WovenBagHWANE21190Item.基本属性组_退包数,cancel)
						.putAttribute(WovenBagHWANE21190Item.基本属性组_破损数,worn)
						.putAttribute(WovenBagHWANE21190Item.基本属性组_剩余数, Math.max(surplus, 0))
				;
//				if(saveSurplus==0){
//					update.putAttribute(WovenBagHWANE21190Item.基本属性组_剩余确认,true);
//				}
				IntegrationMsg msg=update.integrate();
				log.info("更新编织袋管理信息:{},{}",msg.success(),msg.getRefuseMsgStr());
			}
		}
	}
}
